﻿// 2022/4/1: 首个版本
// 2023/2/16: 新增EnableMMapVideoReader。移除MayCauseLowSpeed
// 2023/4/10: 增加Load/Save方法

using System;
using System.Collections.Generic;
using System.Xml;
using ASEva;
using ASEva.Utility;

namespace CommonSource
{
    class WorkerFileIOConfig : ModuleConfig
    {
        public bool EnablePackedPointCloudReader { get; set; }
        public bool EnablePackedPointCloudWriter { get; set; }
        public bool EnableMMapRawReader { get; set; }
        public bool EnableMMapBusReader { get; set; }
        public bool EnableMMapVideoReader { get; set; }
        public bool EnableMMapSignalWriter { get; set; }
        public bool EnableMMapSampleWriter { get; set; }
        public bool EnableMMapPointCloudWriter { get; set; }

        public override void DisableAll()
        {
            EnablePackedPointCloudReader = false;
            EnablePackedPointCloudWriter = false;
            EnableMMapRawReader = false;
            EnableMMapBusReader = false;
            EnableMMapVideoReader = false;
            EnableMMapSignalWriter = false;
            EnableMMapSampleWriter = false;
            EnableMMapPointCloudWriter = false;
        }

        public static WorkerFileIOConfig Load(object caller)
        {
            var config = new WorkerFileIOConfig();
            var configString = Agency.GetModuleConfig(caller, "worker-file-io.pluginworkerfileio");
            if (configString == null) return null;
            config.SetConfig(configString);
            return config;
        }

        public void Save(object caller)
        {
            Agency.SetModuleConfig(caller, "worker-file-io.pluginworkerfileio", GetConfig());
        }

        public override string GetConfig()
        {
            var xml = Xml.Create("root");
            var cw = new AttributeWriter(xml.DocumentElement);

            cw.WriteBool("packed_point_cloud_reader", EnablePackedPointCloudReader, "yes", "no");
            cw.WriteBool("packed_point_cloud_writer", EnablePackedPointCloudWriter, "yes", "no");
            cw.WriteBool("mmap_raw_reader", EnableMMapRawReader, "yes", "no");
            cw.WriteBool("mmap_bus_reader", EnableMMapBusReader, "yes", "no");
            cw.WriteBool("mmap_video_reader", EnableMMapVideoReader, "yes", "no");
            cw.WriteBool("mmap_signal_writer", EnableMMapSignalWriter, "yes", "no");
            cw.WriteBool("mmap_sample_writer", EnableMMapSampleWriter, "yes", "no");
            cw.WriteBool("mmap_point_cloud_writer", EnableMMapPointCloudWriter, "yes", "no");

            return xml.InnerXml;
        }

        public override ConfigStatus GetConfigStatus()
        {
            if (EnablePackedPointCloudReader) return ConfigStatus.Enabled;
            if (EnablePackedPointCloudWriter) return ConfigStatus.Enabled;
            if (EnableMMapRawReader) return ConfigStatus.Enabled;
            if (EnableMMapBusReader) return ConfigStatus.Enabled;
            if (EnableMMapVideoReader) return ConfigStatus.Enabled;
            if (EnableMMapSignalWriter) return ConfigStatus.Enabled;
            if (EnableMMapSampleWriter) return ConfigStatus.Enabled;
            if (EnableMMapPointCloudWriter) return ConfigStatus.Enabled;

            return ConfigStatus.Disabled;
        }

        public override void SetConfig(string config)
        {
            if (String.IsNullOrEmpty(config)) return;

            var xml = new XmlDocument();
            xml.LoadXml(config);
            var cp = new AttributeParser(xml.DocumentElement);

            EnablePackedPointCloudReader = cp.ParseBool("packed_point_cloud_reader", "yes", false);
            EnablePackedPointCloudWriter = cp.ParseBool("packed_point_cloud_writer", "yes", false);
            EnableMMapRawReader = cp.ParseBool("mmap_raw_reader", "yes", false);
            EnableMMapBusReader = cp.ParseBool("mmap_bus_reader", "yes", false);
            EnableMMapVideoReader = cp.ParseBool("mmap_video_reader", "yes", false);
            EnableMMapSignalWriter = cp.ParseBool("mmap_signal_writer", "yes", false);
            EnableMMapSampleWriter = cp.ParseBool("mmap_sample_writer", "yes", false);
            EnableMMapPointCloudWriter = cp.ParseBool("mmap_point_cloud_writer", "yes", false);
        }

        public override List<RecordDataType> GetRecordDataTypes()
        {
            var list = new List<RecordDataType>();
            if (EnablePackedPointCloudWriter || EnableMMapPointCloudWriter) list.Add(RecordDataType.Matrices);
            if (EnableMMapSignalWriter) list.Add(RecordDataType.Signals);
            if (EnableMMapSampleWriter) list.Add(RecordDataType.Samples);
            return list;
        }
    }
}